package com.ly.count.impl.login;
 
import cn.hutool.core.lang.generator.UUIDGenerator;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ly.count.context.CPlusLoginContext;
import com.ly.count.http.CplusHttpStatus;
import com.ly.count.impl.login.transfer.CPlusLoginTransfer;
import com.ly.count.impl.score.transfer.CPlusRoomScoreTransfer;
import com.ly.count.impl.user.transfer.CPlusUserTransfer;
import com.ly.count.login.mapper.CPlusLoginMapper;
import com.ly.count.login.param.CPlusLoginPage;
import com.ly.count.login.param.CPlusLoginParam;
import com.ly.count.login.param.CPlusRegisterParam;
import com.ly.count.login.po.CPlusLogin;
import com.ly.count.service.login.CPlusLoginService;
import com.ly.count.service.user.CPlusUserService;
import com.ly.count.user.po.CPlusUser;
import com.ly.count.utils.LoginContextUtil;
import com.ly.count.utils.ResponseModelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.ly.count.http.CPlusResponse;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * Module: CPlusLogin.java
 * 
 * @author liaoyong
 * @since JDK 1.8
 * @version 1.0
 * @date 2024-10-03
 * @Descriptions:
 */
@Service("cPlusLoginService")
public class CPlusLoginServiceImpl extends ServiceImpl<CPlusLoginMapper, CPlusLogin> implements CPlusLoginService {

	@Autowired
	private CPlusUserService cPlusUserService;

	public CPlusResponse<CPlusLoginContext> login(CPlusLoginParam cPlusLoginParam) {
		if(ObjectUtils.isEmpty(cPlusLoginParam) || StringUtils.isBlank(cPlusLoginParam.getAccount())){
			return ResponseModelUtils.nobody(CplusHttpStatus.LOGIN_PARAM_NPE);
		}
		final CPlusResponse<CPlusUser> userResponse =  this.cPlusUserService.getUser(cPlusLoginParam.getAccount());

		if(userResponse.getData() == null){
			return ResponseModelUtils.nobody(CplusHttpStatus.LOGIN_USER_NOT_FOUND);
		}
		CPlusLogin login = new CPlusLogin();
		login.setId(userResponse.getData().getLoginId());
		login.setPassword(cPlusLoginParam.getPassword());
		Wrapper<CPlusLogin> queryWrapper = new QueryWrapper<>(login);
		if(getOneOpt(queryWrapper).isEmpty()){
			return ResponseModelUtils.nobody(CplusHttpStatus.LOGIN_USERNAME_OR_PASSWORD_ERROR);
		}
		CPlusLoginContext loginContext = createLoginContext(userResponse.getData());

		return ResponseModelUtils.body(loginContext);
	}

	private CPlusLoginContext createLoginContext(CPlusUser user) {
		CPlusLoginContext context = new CPlusLoginContext();
		context.setLoginType(1);
		context.setDevice("0000");
		context.setOrgId(0L);
		final String token = IdUtil.fastUUID();

		context.setToken(token);
		context.setUserName(user.getNickname());
		context.setUserId(user.getId());
		context.setOrgName("组织");

		LoginContextUtil.put(token,context);
		return context;
	}

	public CPlusResponse<Object>	add(CPlusLogin cPlusLogin){
		boolean result = save(cPlusLogin);
		if(result) {		
			return ResponseModelUtils.success();
		}
		return ResponseModelUtils.fail();
	}
	
	public CPlusResponse<Object>	modify(CPlusLogin cPlusLogin){
		CPlusResponse<Object> responseHeaderModel = new CPlusResponse<>(CplusHttpStatus.APP_SUCCESS);
		
		boolean result = updateById(cPlusLogin);
		if(!result) {
			return ResponseModelUtils.fail();
		}
		return ResponseModelUtils.success();
	}


	public CPlusResponse<Page<CPlusLogin>> pageList(CPlusLoginPage  page){

		return ResponseModelUtils.body(page(CPlusLoginTransfer.toPage(page),new QueryWrapper<CPlusLogin>()));

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public CPlusResponse<Object> register(CPlusRegisterParam cPlusRegisterParam) {
		//保存两个实体：用户基本信息和账号登录信息
		CPlusLogin cPlusLogin =CPlusLoginTransfer.toPO(cPlusRegisterParam);
		Long loginId = IdUtil.getSnowflakeNextId();
		cPlusLogin.setId(loginId);
		CPlusUser cPlusUser = CPlusUserTransfer.toPO(cPlusRegisterParam);
		cPlusUser.setId(IdUtil.getSnowflakeNextId());
		cPlusUser.setLoginId(loginId);
		cPlusUserService.save(cPlusUser);
		this.save(cPlusLogin);

		return ResponseModelUtils.success();
	}


}
